#include "ftp.h"
#include <string.h>
#include <iostream>

ftp::ftp() {
    this->value = 4;
    strcpy(this->commands[0], "USER");
    strcpy(this->commands[1], "exit");
    strcpy(this->commands[2], "lala");

    this->actions[0] = (ftp::arrAct) &ftp::tstAct0;
    this->actions[1] = (ftp::arrAct) &ftp::tstAct1;
    //this->actions[2] = (ftp::arrAct) &ftp::tstAct2;


    std::cout << "for examle: " << *ftp::commands[0] << std::endl;
    //exit(0);
}

int ftp::ftpCreateSocket() {
    sockets::sockConfigure();
    std::cout << "create socket... \t" ;
    if(!(sockets::sockCreate())) { std::cout << "FAIL" << std::endl; exit(0); }
    else std::cout << "ok" << std::endl;

    std::cout << "bind socket... \t";
    if(!sockets::sockBind())   { std::cout << "FAIL" << std::endl; exit(0); }
    else std::cout << "ok" << std::endl;

    std::cout << "listen socket." << std::endl;
    std::cout << "port:" << sockets::sockListen() << std::endl;
}

void** parseBuffer_FAIL(char *buffer) {
    char *str, *str1;
    str = new char[strlen(buffer)];


    strcat(str, buffer);
    str1 = new char[strlen(str)];
    strcpy(str1, str);

    int parseNum = 2;
    char *parseStr[parseNum];

    parseStr[0] = new char[8];
    parseStr[1] = new char[8];

    std::cout << "input : " << str << std::endl;
    int  inc = 0;
    strtok(str1, " ");
    do {
        std::cout << "inc: " << inc << std::endl;
        strcpy(parseStr[inc], str1);
        for(int i = strlen(str1); i < (strlen(str)); i++) str[i-strlen(str1)] = str[i+1];
        std::cout << "str1 = " << str1 << ";" << std::endl << "str  = " << str << ";" << std::endl;
        if(inc == (parseNum-1)) {;
            char *parseStrTmp[++parseNum];
            for(int i = 0; i < (parseNum-1); i++) {
                parseStrTmp[i] = new char[8];
                strcpy(parseStrTmp[i], parseStr[i]);
                std::cout << "parseStrTmp[" << i << "] = " <<  parseStrTmp[i] << "; parseStr[] = " << parseStr[i] << std::endl;
                delete parseStr[i];
            }

//            delete [] parseStr;
            for(int i=0; i < parseNum; i++) {
                parseStr[i] = new char[8];
                strcpy(parseStr[i], parseStrTmp[i]);
            }
        } else{
            strcpy(parseStr[inc], str1);
        }
            strcpy(str1,str);

        std::cout << "parseStr[" << inc << "] = " << parseStr[inc] << std::endl;

        inc++;

    } while(strtok(str1, " "));

    return (void**) parseStr;

}

ftp::commEnArg ftp::parseBuffer(char *buffer) {
//    char *tmpBuffer = new char[1024];
//    strcpy(tmpBuffer, buffer);
//    commEnArg resultParsing[2];
//    resultParsing[0] = new char[5];
//    resultParsing[0] = (ftp::commEnArg*) strtok(tmpBuffer, ' ');
//    for(int i = strlen(resultParsing[0]); i < strlen(buffer); i++)
//        buffer[i-strlen(resultParsing[0])] = buffer[i];
//    resultParsing[1] = strtok(buffer, ' ');
//    return resultParsing;
}

int ftp::ftpWaitConnection() {
    if((sockList = accept(sock, NULL, NULL)) < 0) {
        perror("accept");
        exit(3);
    }

    std::cout << "i am feeling connect from someone address:" << std::endl;
    send(sockList, "220 mavrFTP Server\n", 20, 0);

    while(true) {

//        sock = accept(listener, (struct sockaddr *)&addrCl, (socklen_t *) sizeof(addrCl));
//        std::cout << "connect from " << addrCl.sin_addr <<  std::endl;

        memset (buf, 0, 1024);
        bytes_read = recv(sockList, buf, 1023, 0);

        if (bytes_read <= 0)
            break;

        //DUMP(buf);
        std::cout << "from buffer " << buf << std::endl;

        void *pBuff;

        pBuff = (void*) this->parseBuffer(buf);
        for(int i = 0; i <= NUM_COMMANDS - 1; i++) {
//            if(strncmp((char*)pBuff[0], this->commands[i], strlen(this->commands[i])) == 0) {
//                std::cout << "doing: " << this->commands[i] << std::endl;
//                this->actions[i](this);
//            }
         }

    }
}



int ftp::tstAct0(ftp *is) {
    // и это как раз тот момент, когда ты действительно не понимаешь почему эта херня работает
    std::cout << "user say0: " << this->value++ << std::endl;
}

int ftp::tstAct1(ftp *is) {
    std::cout << "user say1: " << this->commands[1] << std::endl;
    close(sock);
}

int ftp::tstAct2(ftp *is) {
    std::cout << "user say2: " << this->commands[2] << std::endl;
    close(sock);
}
